package com.lovo.web.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lovo.commom.CscRemote.ICscUserOrderRemote;
import com.lovo.commom.CscRemote.ICscUserRemote;
import com.lovo.commom.remoteMethod.ISysUserOrderRemote;
import com.lovo.web.bean.RefundBean;
import com.lovo.web.bean.UserBean;
import com.lovo.web.bean.UserOrderBean;
import com.lovo.web.mapper.IRefundMapper;
import com.lovo.web.mapper.IUserMapper;
import com.lovo.web.mapper.IUserOrderMapper;
import com.lovo.web.service.IUserOrderService;
import com.lovo.web.vo.DepositVo;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

@Transactional
@Service
public class UserOrderServiceImpl implements IUserOrderService {
    @Autowired
    IUserOrderMapper userOrderMapper;
    @Autowired
    private IUserOrderMapper orderMapper;
    @Autowired
    private IUserMapper userMapper;
    @Autowired
    ISysUserOrderRemote sysUserOrderRemote;
    @Autowired
    ICscUserOrderRemote cscUserOrderRemote;
    @Autowired
    ICscUserRemote userRemote;
    @Autowired
    IRefundMapper refundMapper;
    @Autowired
    RabbitTemplate rabbitTemplate;

    @Override
    public List<UserOrderBean> findByNumList(List<String> nums) {
        return orderMapper.findByNumList(nums);
    }

    @Override
    public IPage<UserOrderBean> findAllByBuyerName(int pageNO, String buyerName, int status) {
        QueryWrapper<UserOrderBean> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fk_buyerName", buyerName);
        queryWrapper.eq("uo_delete", 0);
        if (status != -1) {
            queryWrapper.eq("uo_status", status);
        }
        return orderMapper.selectPage(new Page<>(pageNO, 5), queryWrapper);
    }

    @Override
    public void del(long orderId) {
        QueryWrapper<UserOrderBean> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pk_orderId", orderId);
        UserOrderBean order = orderMapper.selectOne(queryWrapper);
        order.setIsDelete(1);
        orderMapper.updateById(order);
    }

    @Override
    public void updateOrderStatus(List<String> nums, LocalDate nowDate) {
        orderMapper.updateOrderStatus(nums, nowDate);
    }

    @Override
    public void pay(DepositVo depositVo) {
        List<String> orderNumList = depositVo.getOrderNumList();
        for (String orderNum : orderNumList) {
            QueryWrapper<UserOrderBean> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("uo_orderNum", orderNum);
            UserOrderBean order = orderMapper.selectOne(queryWrapper);
            order.setStatus(1);
            order.setPayDate(LocalDate.now());
            orderMapper.updateById(order);
            sysUserOrderRemote.updateStatus(orderNum, 1, LocalDate.now().toString());
            //远程调用修改审核系统订单状态
            cscUserOrderRemote.CscUpdateStatus(orderNum, 1, LocalDate.now().toString());
        }
        QueryWrapper<UserBean> userBeanQueryWrapper = new QueryWrapper<>();
        userBeanQueryWrapper.eq("u_userName", depositVo.getUserName());
        UserBean user = userMapper.selectOne(userBeanQueryWrapper);
        user.setMoney(user.getMoney() - depositVo.getBuyPrice());
        userMapper.updateById(user);
        //远程调用审核系统更改用户预存款
        userRemote.updateMoneyByUserName(user.getUserName(), -depositVo.getBuyPrice());
    }

    @Override
    public void updateStatus(String orderNum, int status, String content) {
        QueryWrapper<UserOrderBean> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uo_orderNum", orderNum);
        UserOrderBean order = orderMapper.selectOne(queryWrapper);
        if (status == 4) {
            //保存退款详情
            RefundBean refundBean = new RefundBean(orderNum, content);
            refundMapper.insert(refundBean);
            String jsonString = JSON.toJSONString(refundBean);
            //发送退款详情消息队列
            rabbitTemplate.convertAndSend("refundTwoExchange", "cscTwoRefund.key", jsonString);
            rabbitTemplate.convertAndSend("refundTwoExchange", "sysTwoRefund.key", jsonString);
        }
        order.setStatus(status);
        orderMapper.updateById(order);
        //远程调用后台系统更改订单状态
        sysUserOrderRemote.updateStatus(orderNum, status, "2000-01-01");
        //远程调用修改审核系统订单状态
        cscUserOrderRemote.CscUpdateStatus(orderNum, status, "2000-01-01");
    }

    @Override
    public void updateStatusRemote(String orderNum, int status) {
        QueryWrapper<UserOrderBean> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uo_orderNum", orderNum);
        UserOrderBean order = orderMapper.selectOne(queryWrapper);
        order.setStatus(status);
        orderMapper.updateById(order);
    }

    @Override
    public void updateRefund(String orderNum, int status, LocalDate updateDate, String adminName) {
        QueryWrapper<RefundBean> qw = new QueryWrapper<>();
        qw.eq("fk_userOrderIdNum", orderNum).orderByDesc("pk_refundId").last("limit 1");
        RefundBean refundBean = refundMapper.selectOne(qw);
        refundBean.setUpdateDate(updateDate);
        refundBean.setAdminName(adminName);
        refundBean.setAuditState(status);
    }
}
